﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using Common;

namespace VMS_Web
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        
        protected MainMaster CurrentMasterPage = null;

        private Master_Item_Types_DB ObjMaster_Item_Types_DB = null;

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            CurrentMasterPage = (MainMaster)this.Master;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (Request["Mode"] == "Edit" && Request["Item"] != null && Request["Desc"] != null)
                {
                    txtmastertitemtype.Value = Request["Item"].ToString();
                    txtmastertitemdesc.Value = Request["Desc"].ToString();
                    btnAction.InnerText = "Edit";
                }

                else if (Request["Mode"] == "Delete" && Request["Item"] != null)
                {
                    txtmastertitemtype.Value = Request["Item"].ToString();
                    btnAction.InnerText = "Delete";
                }
                else
                {
                    btnAction.InnerText = "Add";
                }
                if (Request["Mode"] == null)
                {
                    btnAction.InnerText = "Add";
                }
            }

            BindGrid();

        }

      
        protected void btnAction_ServerClick(object sender, EventArgs e)
        {
            try
            {

                if (ObjMaster_Item_Types_DB == null)
                {
                    ObjMaster_Item_Types_DB = new Master_Item_Types_DB();
                }



                //To Add new master type
                //For temparaty purpose user_id parameter is given as JitenP. Kindly replace with Session User_ID once login stuff is implemented
                if (Request["Mode"] == null)
                {
                    string restult_message;

                    MethodResult restult = ObjMaster_Item_Types_DB.AddMasterItemType(txtmastertitemtype.Value, txtmastertitemdesc.Value, "JitenP", out restult_message);

                    CurrentMasterPage.MessageToDisplay(restult);
                   
                }

                //To Edit existing master type

                if (Request["Mode"] == "Edit")
                {
                    string restult_message;
                    
                    MethodResult restult = ObjMaster_Item_Types_DB.UpdateMasterItemType(txtmastertitemtype.Value, txtmastertitemdesc.Value, "JitenP", out restult_message);

                    CurrentMasterPage.MessageToDisplay(restult);
                  
                }
                //To Delete existing master type
                if (Request["Mode"] == "Delete")
                {
                    string restult_message;

                    MethodResult restult = ObjMaster_Item_Types_DB.DeleteMasterItemType(txtmastertitemtype.Value, "JitenP", out restult_message);

                    CurrentMasterPage.MessageToDisplay(restult);
                   
                }

                btnCancel_ServerClick(null, null);
                
              }

            catch (Exception exp)
            {
                //1- Log this exception using logger
                CurrentMasterPage.MessageToDisplay(MessageType.Error, "Oops!Some thing went wrong, contact application support team");

            }
        }

        //This procedure is to call when user clicks on Cancel button and after Add,Edit & Delete Procedures.
        protected void btnCancel_ServerClick(object sender, EventArgs e)
        {
            txtmastertitemdesc.Value = "";
            txtmastertitemtype.Value = "";
            btnAction.InnerText = "Add";

        }


     
        private void BindGrid()
        {
            if (ObjMaster_Item_Types_DB == null)
            {
                ObjMaster_Item_Types_DB = new Master_Item_Types_DB();
            }

            grdmasteritemdetails.DataSource = ObjMaster_Item_Types_DB.GetMasterItemTypes();
            grdmasteritemdetails.DataBind();

        }

        public string RedirectForEdit(string item, string desc)
        {
            return "MasterTypes.aspx?Mode=Edit&Item=" + item + "&Desc=" + desc;

        }

        public string RedirectForDelete(string item)
        {
            return "MasterTypes.aspx?Mode=Delete&Item=" + item;

        }

    }
}